package com.xyd.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * 类描述：
 *
 * @author 张吉哲
 * @date 2021/1/14 9:58
 */
public class CookieUtil {

  private static final String COOKIE_NAME = "projectShoppingCarCookie";

  public static String getCookieName() {
    return COOKIE_NAME;
  }

  /**
   * 更新cookie信息
   *
   * @param c        被更新的cookie
   * @param value    更新后的值
   * @param response 必要参数
   */
  public static void fixCookie(Cookie c, String value, HttpServletResponse response) {
    c.setValue(value);
    // 设置保存时间
    c.setMaxAge(60 * 60 * 24 * 365);
    response.addCookie(c);
  }

  /**
   * 添加cookie信息
   *
   * @param value    添加到cookie的值
   * @param response 必要参数
   */
  public static void saveCookie(String value, String cookieName, HttpServletResponse response) {
    Cookie cookie = new Cookie(cookieName + COOKIE_NAME, value);
    cookie.setMaxAge(60 * 60 * 24 * 30);
    cookie.setPath("/");
    response.addCookie(cookie);
  }
}
